package cn.edu.nju.software.gui;

import java.awt.Component;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumnModel;

import cn.edu.nju.software.net.*;


public class CoursesManage extends JPanel implements ActionListener{
	private Connection connection;
	public ManagerOperation mo;
	private JTable coursesExists;
	private JTable coursesToDelete;
	ArrayList<CourseBean> courses = new ArrayList<CourseBean>();//the courses 
	ArrayList<CourseBean> tempCourses = new ArrayList<CourseBean>();;
	ArrayList<Integer> tempForDel = new ArrayList<Integer>();;//����Ҫɾ�������
	ArrayList<CourseBean> coursesToDel = new ArrayList<CourseBean>();;
	ArrayList<Integer> tempForAdd = new ArrayList<Integer>();;
	
	
	ArrayList<CourseBean> coursesToAdd;
	
	public CoursesManage(ManagerOperation newMO){
		mo = newMO;
		connection = Connection.getInstance();
		//init
		courses = connection.handleAdministratorCourseView();// ����һ��Coursebean��ArrayList����
		
		
		for(int i =0 ;i<courses.size();i++){
			tempCourses.add(courses.get(i));
		}//copyһ�����������ʼ��courses
		
		
		int row = courses.size();
		coursesExists = new JTable();//�µ�һ���γ̱�
		coursesToDelete = new JTable();
		
		 final DefaultTableModel ce_dtm  = (DefaultTableModel)coursesExists.getModel() ; 
		  coursesExists.setModel(ce_dtm); 
		  final Object[] columnNames = {"id", "name", "institute",
				   "teacher", "grade", "number", "place", "shared"};
		  ce_dtm.setColumnIdentifiers(columnNames); 
		  
		  
		  //���������Ϲ�����
		  JScrollPane pane1 = new JScrollPane (coursesExists);
		  JScrollPane pane2 = new JScrollPane (coursesToDelete);
		  
		  
		  for(int i = 1;i<=row;i++){
		        
		        ce_dtm.addRow(new Object[] {
		        		courses.get(i-1).getId(),
		        		courses.get(i-1).getName(),
		        		courses.get(i-1).getInstitute(),
		        		courses.get(i-1).getTeacher(),
		        		courses.get(i-1).getGrades(),
		        		courses.get(i-1).getNumber(),
		        		courses.get(i-1).getPlace(),
		        		courses.get(i-1).getShared(),
		        		});   
		          }//�����пγ�һ��һ����ʾ����
		  ce_dtm.fireTableDataChanged();//repaint the table
		  
		  final DefaultTableModel ctd_dtm =  (DefaultTableModel)coursesToDelete.getModel() ; 
		  coursesToDelete.setModel(ctd_dtm);
		  ctd_dtm.setColumnIdentifiers(columnNames); //�������ǲ��ɱ༭��
		  
		
		JLabel establishedLabel = new JLabel("courses Established");
		JPanel establishedPanelforTable = new JPanel();//�����������пγ̵ı�
		establishedPanelforTable.add(pane1);
		
		JPanel toDeletePanel = new JPanel();
		toDeletePanel.setLayout(new GridLayout(1,2));
		JLabel toDeleteLabel = new JLabel("courses to Delete");
		JButton down = new JButton("down");
		down.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				int selectRow = coursesExists.getSelectedRow();//��ȡ���е�����   0����1Ҫע����
				// TODO Auto-generated method stub
				tempForDel.add(selectRow);
				coursesToDel.add(courses.get(selectRow));
				ctd_dtm.addRow(new Object[] {
		        		courses.get(selectRow-1).getId(),
		        		courses.get(selectRow-1).getName(),
		        		courses.get(selectRow-1).getInstitute(),
		        		courses.get(selectRow-1).getTeacher(),
		        		courses.get(selectRow-1).getGrades(),
		        		courses.get(selectRow-1).getNumber(),
		        		courses.get(selectRow-1).getPlace(),
		        		courses.get(selectRow-1).getShared(),
		        		});
				ctd_dtm.fireTableDataChanged();//repaint the table
			}
			
		});
		
		
		//ctd_dtm.fireTableDataChanged();//ˢ����ʾ
		toDeletePanel.add(toDeleteLabel);
		toDeletePanel.add(down);
		
		JPanel toDeletePanelforTable = new JPanel();//��������׼��ɾ��γ̵ı�
		toDeletePanelforTable.add(pane2);
		
		
		JPanel deleteButtonPanel = new JPanel();
		JButton deleteButton = new JButton("Delete");
		deleteButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				if(connection.handleDeleteCourse(coursesToDel)){
					JOptionPane.showMessageDialog(null,"Delete successfully!");//�ɹ�ɾ��
					for(int i =0;i<tempForDel.size();i++){
					ce_dtm.removeRow(tempForDel.get(i));//���γ̴����б����Ƴ�
					ctd_dtm.removeRow(i+1);//���ѡ����ʱ��
					ce_dtm.fireTableDataChanged();//repaint the table
					ctd_dtm.fireTableDataChanged();//repaint the table
					}
					tempForDel.clear();//��ʱ�������
				}
				else{
					JOptionPane.showMessageDialog(null,"Fail to Delete!");//ɾ��ʧ��
					for(int i =0;i<tempForDel.size();i++)
						ctd_dtm.removeRow(i+1);//���ѡ����ʱ��
					ctd_dtm.fireTableDataChanged();//repaint the table
					tempForDel.clear();//��ʱ�������
				}
			}
			
		});
		deleteButtonPanel.add(deleteButton);
		
		
	    
		
		JLabel toAddLabel = new JLabel("to add:");
		JButton continueAddButton = new JButton("continue to add");
		JPanel PanelforLabelAndButton = new JPanel();
		PanelforLabelAndButton.add(toAddLabel);
		PanelforLabelAndButton.add(continueAddButton);
		
		final AddCoursesPanel acp = new AddCoursesPanel();//���һ�����panel
		
		
		continueAddButton.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				if(acp.checkInput()== true){//ÿһ����ݶ���ֵ��
				   if(acp.checkShared()){//share������Ҳ��ȷ
				    coursesToAdd.add(acp.getCourse());
				    acp.clear();
				    }
				    else
					JOptionPane.showMessageDialog(null, "The value of Shared can only be Yes or No");
				}
				   else
					   JOptionPane.showMessageDialog(null,	"There are empty items!");
			}
			
		});
		
		
		
		
		
		JPanel addButtonPanel = new JPanel();
		JButton addButton = new JButton("Add");
		addButtonPanel.add(addButton);
		addButton.addActionListener(new ActionListener(){
            @Override
			public void actionPerformed(ActionEvent e) {
            	if(acp.checkInput()&&acp.checkShared()){//˵����ǰ��ӵ����һ��Ҳ����������
            		coursesToAdd.add(acp.getCourse());
            		if(connection.handleAddCourse(coursesToAdd)){
            			JOptionPane.showMessageDialog(null, "Add successfully");
            		//ˢ          ��        ��            ʾ 
            		}
            		
            		else{
            			JOptionPane.showMessageDialog(null, "Fail to Add");
            		}
            		acp.clear();	
            			
            	}
            	else{
            		JOptionPane.showMessageDialog(null, "Please ensure the inputs are correct");
            	}
            
				// TODO Auto-generated method stub
				
			}
			
		});
		
GridLayout gridLayout = new GridLayout(8,0);//���panel�Ĳ���
		
		setLayout(gridLayout);
		add(establishedLabel);
		add(establishedPanelforTable);
		add(toDeletePanel);
		add(toDeletePanelforTable);
		add(deleteButtonPanel);
		add(PanelforLabelAndButton);
		add(acp);
		add(addButtonPanel);
		
		
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		
	}

}

